/**
 * Copyright (c) 2013-5-2 下午3:18:05 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.action.music;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.extblog.wkm.web.action.ActionController;
import org.extblog.wkm.web.service.music.MusicService;
import org.extblog.wkm.web.vo.MusicFormView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author WKM
 * Service.java history
 * 1.WKM - 2013-5-2 - Create File
 */
@Controller
@RequestMapping("/music")
public class MusicController extends ActionController {

	/** 音乐业务处理逻辑 **/
	private MusicService musicService = null;
	
	public MusicService getMusicService() {
		return musicService;
	}

	@Resource
	public void setMusicService(MusicService musicService) {
		this.musicService = musicService;
	}

	@RequestMapping("home.wkm")
	public ModelAndView home(){
		ModelAndView mav = new ModelAndView();
		log.info("跳转到页面添加页面...");
		mav.setViewName("music/musicadd");
		mav.addObject("messageForm0", "添加音乐");
		return mav;
	}
	
	@RequestMapping("edit.wkm")
	public ModelAndView edit(){
		ModelAndView mav = new ModelAndView();
		log.info("跳转到页面添加页面...");
		mav.setViewName("music/musicedit");
		return mav;
	}
	
	@RequestMapping("addMusic.wkm")
	public ModelAndView addMusic(@ModelAttribute("view") MusicFormView view){
		ModelAndView mav = new ModelAndView();
		log.info("添加音乐元素...");
		mav.setViewName("music/musicadd");
		if(musicService.addMusic(view)){
			mav.addObject("messageForm0", "操作成功！");
		} else {
			mav.addObject("messageForm0", "操作失败！");
		}
		return mav;
	}
	
	@RequestMapping("updateMusic.wkm")
	@ResponseBody
	public Object updateMusic(@ModelAttribute("view") MusicFormView view){
		log.info("更新数据ID=" + view.getMusicId() + ";Name=" + view.getTitle());
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getMusicId() == null){
			log.info("ID=null不能更新数据...");
			map.put("success", String.valueOf(Boolean.FALSE));
		} else {
			if(musicService.updateMusicById(view)){
				log.info("更新ID=" + view.getMusicId() + "数据成功.");
				map.put("success", String.valueOf(Boolean.TRUE));
			} else {
				log.info("更新ID=" + view.getMusicId() + "数据失败.");
				map.put("success", String.valueOf(Boolean.FALSE));
			}
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("deleteMusic.wkm")
	@ResponseBody
	public Object deleteMusic(@ModelAttribute("view") MusicFormView view){
		log.info("删除数据ID=" + view.getMusicId() + ";Name=" + view.getTitle());
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getMusicId() == null){
			log.info("ID=null不能删除数据...");
			map.put("success", String.valueOf(Boolean.FALSE));
		} else {
			if(musicService.removeMusicById(view.getMusicId())){
				log.info("删除ID=" + view.getMusicId() + "数据成功.");
				map.put("success", String.valueOf(Boolean.TRUE));
			} else {
				log.info("删除ID=" + view.getMusicId() + "数据失败.");
				map.put("success", String.valueOf(Boolean.FALSE));
			}
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	} 
	
	@RequestMapping("loadMusic.wkm")
	@ResponseBody
	public Object loadMusic(){
		log.info("加载所有的音乐信息...");
		Map<String,Object> map = new HashMap<String, Object>();
		List<Map<String,String>> list = musicService.getMusics();
		if(list != null){
			map.put("roots", list);
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
}
